// pages/personal/index.js
import md5 from "../../utils/md5.js"
const net = require('../../utils/net.js')
import {inputgetName} from "../../utils/inputBothBind.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePaths:'0',
    name: "",
    carded: "",
    stepText: ["1.身份认证", "2.储蓄卡认证"],
    cardType: {
      name: '',
      id: 10
    },
    cardNum: '',
    validDt: ''
  },

  inputgetName: inputgetName,
  toSelectCardType: function () {
    console.log("66666")
    wx.navigateTo({
      // TODO
      url: ""
    })
  },
  toBankCardOCR: function () {
    console.log("bankcard OCR")
    // TODO
  },
  toSelectValidDt: function () {
    // TODO
    console.log("选择有效期")
  },
  toNext: function () {
    console.log(this.data.cardNum.length)
    console.log(this.data.validDt)
    let uid = wx.getStorageSync("xlk_uid")

    if (this.data.cardNum.length < 2) {
      wx.showToast({
        title: '姓名格式有误',
        duration: 2000,
        icon: true
      });
      return false;
    }
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (reg.test(this.data.validDt) === false) {
      wx.showToast({
        title: '身份证格式有误',
        duration: 2000,
        icon: true
      });
      return false;
    } 

    wx.navigateTo({
      url: `/pages/authentication2/index?name=${this.data.cardNum}&validDt=${this.data.validDt}&uid=${uid}`,
    })
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  upShopLogo: function () {
      var that = this;
      wx.showActionSheet({
         itemList: ['从相册中选择', '拍照'],
         itemColor: "#f7982a",
         success: function (res) {
          console.log(res);
            if (!res.cancel) {
               if (res.tapIndex == 0) {
                  that.chooseWxImageShop('album')
               } else if (res.tapIndex == 1) {
                  that.chooseWxImageShop('camera')
               }
            }
         }
      })
   },
   chooseWxImageShop: function (type) {
      var that = this;
      wx.chooseImage({
         sizeType: ['original', 'compressed'],
         sourceType: [type],
         success: function (res) {
           var tempFilesSize = res.tempFiles[0].size;
           if (tempFilesSize <= 4190000) {   //图片小于或者等于2M时 可以执行获取图片
             console.log(res);
             that.upload_file(res.tempFilePaths[0])
           } else {    //图片大于2M，弹出一个提示框
             wx.showToast({
               title: '上传图片不能大于4M!',  //标题
               icon: 'none'       //图标 none不使用图标，详情看官方文档
             })
           }
          
        
         /*上传多张（遍历数组，一次传一张）
            for (var index in res.tempFilePaths) {
               that.upload_file(API_URL + 'shop/shopImage', res.tempFilePaths[index])
            }
        */
         }
      })
   },
  upload_file: function (filePath) {
      var that = this;
      var data = {};
      let params = md5(data);
    let token = wx.getStorageSync("xlk_token")
    let uid = wx.getStorageSync("xlk_uid")
    console.log(params);
    wx.uploadFile({
      url: 'http://www.a.com/api/index/index?method=identity',
        filePath: filePath,
        name: 'file',
        method:'POST',
         header: {
           "token": token,
           "uid": uid,
           'edition':'1.3.4',
           "model": "web",
           "platform": "xinlianka"
         }, // 设置请求的 header
         formData: {...params}, // HTTP 请求中其他额外的 form data
         success: function (res) {
           console.log(res.data);
           if (res.data == 1){
             wx.showToast({
               title: '上传图片未识别到信息，请重试',  //标题
               icon: 'none',       //图标 none不使用图标，详情看官方文档
               duration: 3000
             })
           }else{
             var a = res.data.split(',');
             var s1 = a['0'].replace('"', '');
             var b1 = unescape(s1.replace(/\\u/g, "%u"));
             var sd = a['1'];
             var b2 = sd.replace('"', '');
             that.setData({
               cardNum: b1,
               validDt: b2
             })
             wx.showToast({
               title: "图片识别成功",
               icon: 'success',
               duration: 700
             })
           }
           
         },
         fail: function (res) {

         }
      })
   },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})